import Vue from "vue";
import Vuex from "vuex";
import axios from "axios";
import getAsyncRoutes from "../router/asyncRouter"

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    Dict: "",
    MenuList: JSON.parse(localStorage.getItem("MenuArr")) || [],
    userName: localStorage.getItem("userName") || ""
  },
  mutations: {
    GET_DICT: (state, val) => {
      state.Dict = val;
    },
    SET_MENUS:(state, val) => {
      state.MenuList = val;
    },
    USER_NAME:(state, val) => {
      state.userName = val;
      localStorage.setItem("userName",val)
    },
  },
  actions: {
    //线上去掉/sita/whale/
    getDict(context, params) {
      axios
        .post("/sita/whale/dictionary/queryList", params, {
          headers: {
            Authorization: localStorage.getItem("token") || ""
          }
        })
        .then(res => {
          console.log(res.data.data);
          this.commit("GET_DICT", res.data.data);
        });
    },
    // 获取菜单
    GetMenu(state, params) {
      axios
        .get(`/sita/akita/menu/getTree/${params.menuId}`, {
          headers: {
            Authorization: localStorage.getItem("token") || ""
          }
        })
        .then(res => {    
        let Menus = res.data.data.menu.treeList
        let UserName = res.data.data.user.userName     
        this.commit("USER_NAME", UserName);   
        getAsyncRoutes(Menus)       
      }).catch(err=>{
        console.log(err.response)
        let url = ''
        Vue.prototype.$Modal.confirm({
          title: "提示",
          width: "322px",
          height: "145px",
          content: "当前暂无权限",
          onOk: async () => {
            if (process.env.NODE_ENV === "test") {
              url = "http://dev-mscar-leasefront.mscar.cn/#/login";
            } else {
              url = "http://172.28.40.200:81/#/login";
            }
            window.name = "";
            window.location.href = url;
            localStorage.removeItem('token')
          }
        });
      })   
    }
  },
});
